package cn.com.wangluotongxin.merchants.service.impl;

import cn.com.wangluotongxin.admin.eureka.AdminClient;
import cn.com.wangluotongxin.admin.eureka.form.GoodsInfoRecallForm;
import cn.com.wangluotongxin.admin.eureka.form.GoodsInfoReleaseForm;
import cn.com.wangluotongxin.common.constants.CommonConstant;
import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.merchants.dto.GoodsInfoMxDto;
import cn.com.wangluotongxin.merchants.model.*;
import cn.com.wangluotongxin.merchants.mapper.MerchantsGoodsInfoMxMapper;
import cn.com.wangluotongxin.merchants.model.constants.GoodsInfoMxAuditStatus;
import cn.com.wangluotongxin.merchants.model.constants.YesNo;
import cn.com.wangluotongxin.merchants.rabbitmq.pojo.GoodsInfoAuditEnd;
import cn.com.wangluotongxin.merchants.service.*;
import cn.com.wangluotongxin.merchants.vo.GoodsInfoMxVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 商户管理-商品信息明细 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-09-12
 */
@Service
public class MerchantsGoodsInfoMxServiceImpl extends ServiceImpl<MerchantsGoodsInfoMxMapper, MerchantsGoodsInfoMx> implements IMerchantsGoodsInfoMxService {

    @Autowired
    private IMerchantsGoodsColorService goodsColorService;

    @Autowired
    private IMerchantsGoodsUnitService goodsUnitService;

    @Autowired
    private IMerchantsGoodsSizeService goodsSizeService;

    @Autowired
    private IMerchantsGoodsInfoService goodsInfoService;

    @Autowired
    private IMerchantsGoodsStandardService goodsStandardService;

    @Autowired
    private AdminClient adminClient;

    @Override
    public IPage<GoodsInfoMxVo> getPage(GoodsInfoMxDto dto) {
        Page page = Page.of(dto.getPageNumber(), dto.getPageSize());
        return baseMapper.getPage(page, dto);
    }

    @Override
    public MerchantsGoodsInfoMx getByStandard(Integer standardId) {
        return this.getOne(new LambdaQueryWrapper<MerchantsGoodsInfoMx>()
                .eq(MerchantsGoodsInfoMx::getStandardId, standardId)
                .last("limit 0,1"));
    }

    @Override
    public void syncRecord(Integer standardId, Integer goodsId) {
        MerchantsGoodsInfo goodsInfo = goodsInfoService.getById(goodsId);

        MerchantsGoodsStandard goodsStandard = goodsStandardService.getById(standardId);

        MerchantsGoodsInfoMx goodsInfoMx = get(goodsInfo, goodsStandard);
        if(goodsInfoMx.getId() != null && goodsInfoMx.getId() > 0) {
            this.updateById(goodsInfoMx);
        } else {
            this.save(goodsInfoMx);
        }
    }

    @Override
    public void release(Integer id) {
        MerchantsGoodsInfoMx goodsInfoMx = this.getById(id);
        Assert.isTrue(!GoodsInfoMxAuditStatus.PASS.getCode().equals(goodsInfoMx.getAuditStatus())
        && !GoodsInfoMxAuditStatus.REJECT.getCode().equals(goodsInfoMx.getAuditStatus()), "商品已审核，不支持发布!");

        GoodsInfoReleaseForm goodsInfoReleaseForm = new GoodsInfoReleaseForm()
                .setTenantId(goodsInfoMx.getTenantId())
                .setGoodsId(goodsInfoMx.getGoodsId())
                .setGoodsNo(goodsInfoMx.getGoodsNo())
                .setGoodsName(goodsInfoMx.getGoodsName())
                .setQuickQueryId(goodsInfoMx.getQuickQueryId())
                .setQuickQueryName(goodsInfoMx.getQuickQueryName())
                .setCatalogId(goodsInfoMx.getCatalogId())
                .setCatalogName(goodsInfoMx.getCatalogName())
                .setCatalogPath(goodsInfoMx.getCatalogPath())
                .setGoodsImages(goodsInfoMx.getGoodsImages())
                .setStandardId(goodsInfoMx.getStandardId())
                .setStandardName(goodsInfoMx.getStandardName())
                .setColorId(goodsInfoMx.getColorId())
                .setColorName(goodsInfoMx.getColorName())
                .setSizeId(goodsInfoMx.getSizeId())
                .setSizeName(goodsInfoMx.getSizeName())
                .setUnitId(goodsInfoMx.getUnitId())
                .setUnitName(goodsInfoMx.getUnitName())
                .setOriPrice(goodsInfoMx.getOriPrice())
                .setSalePrice(goodsInfoMx.getSalePrice())
                .setStockNumber(goodsInfoMx.getStockNumber())
                .setSaleNumber(goodsInfoMx.getSaleNumber())
                .setGoodsRemark(goodsInfoMx.getGoodsRemark())
                .setStandardImages(goodsInfoMx.getStandardImages())
                .setStandardRemark(goodsInfoMx.getStandardRemark());
        Integer isSale = CommonConstant.ONE;
        if(YesNo.YES.getCode().equals(goodsInfoMx.getGoodsIsDeleted()) || YesNo.NO.getCode().equals(goodsInfoMx.getGoodsIsEnabled())
                || YesNo.NO.getCode().equals(goodsInfoMx.getIsSale())) {
            isSale = CommonConstant.ZERO;
        }
        goodsInfoReleaseForm.setIsSale(isSale);
        AjaxResult result = adminClient.goodsInfoRelease(goodsInfoReleaseForm);
        if(!result.isSuccess()) {
            throw new BadResponseException(result.getMessage());
        }

        this.update(new LambdaUpdateWrapper<MerchantsGoodsInfoMx>()
                .set(MerchantsGoodsInfoMx::getAuditStatus, GoodsInfoMxAuditStatus.AUDIT.getCode())
                .set(MerchantsGoodsInfoMx::getAuditMessage, "提交成功，审核中！")
                .set(MerchantsGoodsInfoMx::getIsRelease, YesNo.YES.getCode())
                .eq(MerchantsGoodsInfoMx::getId, goodsInfoMx.getId()));
    }

    @Override
    public void auditEnd(GoodsInfoAuditEnd info) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsInfoMx>()
                .set(MerchantsGoodsInfoMx::getAuditStatus, CommonConstant.ONE.equals(info.getAuditStatus())? GoodsInfoMxAuditStatus.PASS.getCode() : GoodsInfoMxAuditStatus.REJECT.getCode())
                .set(MerchantsGoodsInfoMx::getAuditMessage, info.getAuditRemark())
                .eq(MerchantsGoodsInfoMx::getGoodsId, info.getGoodsId())
                .eq(MerchantsGoodsInfoMx::getStandardId, info.getStandardId()));
    }

    @Override
    public void recall(Integer id) {
        MerchantsGoodsInfoMx goodsInfoMx = this.getById(id);
        Assert.isTrue(goodsInfoMx.getAuditStatus().equals(GoodsInfoMxAuditStatus.AUDIT.getCode()), "数据不支持该操作");

        GoodsInfoRecallForm form = new GoodsInfoRecallForm()
                .setTenantId(goodsInfoMx.getTenantId())
                .setGoodsId(goodsInfoMx.getGoodsId())
                .setStandardId(goodsInfoMx.getStandardId());
        AjaxResult result = adminClient.goodsInfoRecall(form);
        if(!result.isSuccess()) {
            throw new BadResponseException(result.getMessage());
        }

        this.update(new LambdaUpdateWrapper<MerchantsGoodsInfoMx>()
                .set(MerchantsGoodsInfoMx::getAuditStatus, GoodsInfoMxAuditStatus.RECALL.getCode())
                .set(MerchantsGoodsInfoMx::getIsRelease, CommonConstant.ZERO)
                .eq(MerchantsGoodsInfoMx::getId, goodsInfoMx.getId()));
    }

    private MerchantsGoodsInfoMx get(MerchantsGoodsInfo goodsInfo, MerchantsGoodsStandard goodsStandard) {
        MerchantsGoodsInfoMx goodsInfoMx = this.getByStandard(goodsStandard.getId());

        MerchantsGoodsInfoMx newGoodsInfoMx = new MerchantsGoodsInfoMx();
        if(goodsInfoMx != null) {
            newGoodsInfoMx.setId(goodsInfoMx.getId());
        }
        newGoodsInfoMx.setGoodsId(goodsInfo.getId());
        newGoodsInfoMx.setGoodsNo(goodsInfo.getGoodsNo());
        newGoodsInfoMx.setGoodsName(goodsInfo.getGoodsName());
        newGoodsInfoMx.setQuickQueryId(goodsInfo.getQuickQueryId());
        newGoodsInfoMx.setQuickQueryName(goodsInfo.getQuickQueryName());
        newGoodsInfoMx.setCatalogId(goodsInfo.getCatalogId());
        newGoodsInfoMx.setCatalogPath(goodsInfo.getCatalogPath());
        newGoodsInfoMx.setCatalogName(goodsInfo.getCatalogName());
        newGoodsInfoMx.setGoodsImages(goodsInfo.getGoodsImages());
        newGoodsInfoMx.setGoodsRemark(goodsInfo.getRemark());
        newGoodsInfoMx.setGoodsIsEnabled(goodsInfo.getIsEnabled());
        newGoodsInfoMx.setGoodsIsDeleted(goodsInfo.getIsDeleted());
        newGoodsInfoMx.setGoodsCreateUser(goodsInfo.getCreateUser());
        newGoodsInfoMx.setGoodsCreateName(goodsInfo.getCreateName());
        newGoodsInfoMx.setGoodsName(goodsInfo.getGoodsName());
        newGoodsInfoMx.setGoodsModifyUser(goodsInfo.getModifyUser());
        newGoodsInfoMx.setGoodsModifyName(goodsInfo.getModifyName());
        newGoodsInfoMx.setGoodsCreateTime(goodsInfo.getCreateTime());
        newGoodsInfoMx.setGoodsModifyTime(goodsInfo.getModifyTime());
        newGoodsInfoMx.setStandardId(goodsStandard.getId());
        newGoodsInfoMx.setStandardName(goodsStandard.getStandardName());

        if(goodsStandard.getColorId() != null) {
            MerchantsGoodsColor goodsColor = goodsColorService.getById(goodsStandard.getColorId());
            newGoodsInfoMx.setColorId(goodsColor.getId());
            newGoodsInfoMx.setColorName(goodsColor.getColorName());
        }

        if(goodsStandard.getSizeId() != null) {
            MerchantsGoodsSize goodsSize = goodsSizeService.getById(goodsStandard.getSizeId());
            newGoodsInfoMx.setSizeId(goodsSize.getId());
            newGoodsInfoMx.setSizeName(goodsSize.getSizeName());
        }

        if(goodsStandard.getUnitId() != null) {
            MerchantsGoodsUnit goodsUnit = goodsUnitService.getById(goodsStandard.getUnitId());
            newGoodsInfoMx.setUnitId(goodsUnit.getId());
            newGoodsInfoMx.setUnitName(goodsUnit.getUnitName());
        }

        newGoodsInfoMx.setOriPrice(goodsStandard.getOriPrice());
        newGoodsInfoMx.setSalePrice(goodsStandard.getSalePrice());
        newGoodsInfoMx.setIsSale(goodsStandard.getIsSale());
        newGoodsInfoMx.setSaleReason(goodsStandard.getSaleReason());
        newGoodsInfoMx.setStockNumber(goodsStandard.getStockNumber());
        newGoodsInfoMx.setSaleNumber(goodsStandard.getSaleNumber());
        newGoodsInfoMx.setStandardCreateUser(goodsStandard.getCreateUser());
        newGoodsInfoMx.setStandardCreateName(goodsStandard.getCreateName());
        newGoodsInfoMx.setStandardCreateTime(goodsStandard.getCreateTime());
        newGoodsInfoMx.setStandardModifyUser(goodsStandard.getModifyUser());
        newGoodsInfoMx.setStandardModifyName(goodsStandard.getModifyName());
        newGoodsInfoMx.setStandardModifyTime(goodsStandard.getModifyTime());
        newGoodsInfoMx.setIsRelease(YesNo.NO.getCode());
        newGoodsInfoMx.setAuditStatus(GoodsInfoMxAuditStatus.WAIT.getCode());
        newGoodsInfoMx.setStandardRemark(goodsStandard.getRemark());
        newGoodsInfoMx.setStandardImages(goodsStandard.getStandardImages());
        return newGoodsInfoMx;
    }
}
